<?php
/**
 * Created by PhpStorm.
 * User: W
 * Date: 2018/12/21
 * Time: 10:28
 */

namespace app\sports_admin\controller;
use think\Request;
use app\sports_admin\model\Dissertation as DissertationModel;
use app\exception\DissertationException;

class Dissertation extends Base
{
    protected $beforeActionList = [
        'isResource'=>['only'=>'index,paperlist,addpage,addpaper,content,upcontent,delpaper'],
        'operationSelect'=>['only'=>'index.paperlist,addpage,content'],
        'operationInsert'=>['only'=>'addpaper'],
        'operationUpdate'=>['only'=>'upcontent'],
        'operationDelete'=>['only'=>'delpaper']
    ];
    public function index()
    {
        return view("index");
    }
    //列表展示论文
    public function paperList()
    {
        $params = Request::instance()->only(['page','rows','sort','sortOrder','searchName']);
        $page = intval($params['page']) ? intval($params['page']) : 1;
        $rows = intval($params['rows']) ? intval($params['rows']) : 15;
        $sort = trim($params['sort']) ? trim($params['sort']) : 'id';
        $sortOrder = trim($params['sortOrder']) ? trim($params['sortOrder']) : 'asc';
        $searchName = trim($params['searchName']);
        $result = DissertationModel::showListAll($page,$rows,$sort,$sortOrder,$searchName);
        return json($result);
    }
    //添加论文的页面
    public function addPage()
    {
        return view("add");
    }
    //添加论文
    public function addPaper(Request $request)
    {
        if($request->isAjax()){
            $data = DissertationModel::add();
            if($data){
                return json(['code'=>200,'msg'=>'添加成功']);
            }else{
                throw new DissertationException(['code'=>400,'msg'=>"添加失败"]);
            }
        }
    }
    //论文详细信息
    public function content()
    {
        $id = Request::instance()->only("id");
        $data = DissertationModel::where('id',$id['id'])->find();
        return view('update',compact("data"));
    }
    //论文
    public function upContent(Request $request)
    {
        if($request->isAjax()){
            $data = DissertationModel::upContent();
            if($data){
                return json(['code'=>200,'msg'=>'修改成功']);
            }else{
                throw new DissertationException(['errorCode'=>400,'msg'=>"修改失败"]);
            }
        }
    }
    //删除论文
    public function delPaper(Request $request)
    {
        if($request->isAjax()){
            $id = Request::instance()->only('id');
            $is_del = DissertationModel::where("id",$id['id'])->delete();
            if($is_del){
                return json(['code'=>200,'msg'=>'删除成功']);
            }else{
                throw new DissertationException(['errorCode'=>400,'msg'=>"删除失败"]);
            }
        }
    }
}